package eyal.com;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;

public class ApiDemos extends Activity {
    
	private RefreshStateMachine stateMachine = null;
	private MediaPlayer player = null;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        
		super.onCreate(savedInstanceState);
				
		// then, handle the UI
		setContentView(R.layout.main);
		
		// initialize state machine
		LinearLayout ll = (LinearLayout)findViewById(R.id.main_screen);
		player = MediaPlayer.create(getApplicationContext(),R.raw.september);
		player.start();
		stateMachine = new RefreshStateMachine(ll,player);
		stateMachine.setState(RefreshState.NONE);
		
		// retrieve both check boxes
		final CheckBox musicCheckBox = (CheckBox) findViewById(R.id.MusicCheckBox);
		final CheckBox backGroundCheckBox = (CheckBox) findViewById(R.id.BackgroundcheckBox);
		
		// music check box is disabled by default
		if (musicCheckBox.isChecked()) {
			musicCheckBox.setChecked(false);
		}
		
		// background check box is disabled by default
		if (backGroundCheckBox.isChecked()) {
			backGroundCheckBox.setChecked(false);
		}
		
		// retrieve refresh button
		Button refreshButton = (Button) findViewById(R.id.goButton);
						
		// set button functionality
		refreshButton.setOnClickListener(new View.OnClickListener() {
			private boolean flag = false;
			@Override
			public void onClick(View v) {
				boolean music = musicCheckBox.isChecked();; 
				boolean background = backGroundCheckBox.isChecked();
				
				boolean both = music && background;
				boolean any = music || background;
				
				RefreshState newState = null;
				
				do {
					if (both) {
						newState = RefreshState.MUSIC_AND_BACKGROUND;
						break;
					}
					
					if (!any) {
						newState = RefreshState.NONE;
						break;
					}
					
					newState = music ? RefreshState.MUSIC : RefreshState.BACKGROUND;
				}
				while (false);

				stateMachine.setState(newState);
			}
		});
	}
    
    @Override
	protected void onDestroy(){
    	player.release();
    	player = null;
    }

}